<template>
  <el-card class="stats-card">
    <template #header>
      <div class="card-header">
        <span>学习统计</span>
        <el-button type="primary" link @click="refreshStats">
          <el-icon><Refresh /></el-icon>
        </el-button>
      </div>
    </template>
    
    <el-row :gutter="20">
      <el-col :span="6" v-for="(stat, index) in stats" :key="index">
        <div class="stat-item">
          <div class="stat-value">{{ stat.value }}</div>
          <div class="stat-label">{{ stat.label }}</div>
        </div>
      </el-col>
    </el-row>
    
    <div class="progress-section">
      <h4>学习进度</h4>
      <el-progress 
        :percentage="learningProgress" 
        :format="format"
        status="success"
      />
    </div>
  </el-card>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { Refresh } from '@element-plus/icons-vue'
import { useQuestionStore } from '../stores/question'

const questionStore = useQuestionStore()
const statistics = ref({})

const stats = computed(() => [
  { label: '总问题数', value: statistics.value.totalQuestions || 0 },
  { label: '已学会', value: statistics.value.learnedCount || 0 },
  { label: '已收藏', value: statistics.value.collectedCount || 0 },
  { label: '平均难度', value: (statistics.value.averageDifficulty || 0).toFixed(1) }
])

const learningProgress = computed(() => {
  if (!statistics.value.totalQuestions) return 0
  return Math.round((statistics.value.learnedCount / statistics.value.totalQuestions) * 100)
})

const format = (percentage) => `${percentage}% 完成`

const refreshStats = async () => {
  statistics.value = await questionStore.getStatistics()
}

onMounted(refreshStats)
</script>

<style scoped>
.stats-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stat-item {
  text-align: center;
  padding: 20px 0;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-top: 8px;
}

.progress-section {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #EBEEF5;
}

.progress-section h4 {
  margin: 0 0 10px;
  color: #606266;
}
</style> 